<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修复测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-result {
            margin: 10px 0;
            padding: 10px;
            border-radius: 5px;
        }
        .success {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .info {
            background: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
        }
        button {
            padding: 10px 20px;
            margin: 5px;
            border: none;
            border-radius: 3px;
            background: #007bff;
            color: white;
            cursor: pointer;
        }
        button:hover {
            background: #0056b3;
        }
    </style>
</head>
<body>
    <h1>🔧 修复验证测试</h1>
    
    <div class="test-result info">
        <h3>测试目标</h3>
        <p>验证 "document is not defined" 错误已修复</p>
    </div>

    <button onclick="testStrategySelection()">测试策略选择</button>
    <button onclick="testMemoryDetection()">测试内存检测</button>
    <button onclick="testWorkerAvoidance()">测试Worker规避</button>

    <div id="testResults"></div>

    <script>
        // 模拟优化器类的关键方法
        class TestOptimizer {
            constructor() {
                this.maxMemoryUsage = 256 * 1024 * 1024;
            }

            getAvailableMemory() {
                if ('memory' in performance) {
                    return performance.memory.jsHeapSizeLimit - performance.memory.usedJSHeapSize;
                }
                return this.maxMemoryUsage;
            }

            selectProcessingStrategy(fileSize, availableMemory) {
                const fileSizeGB = fileSize / (1024 * 1024 * 1024);

                if (fileSize < availableMemory * 0.3) {
                    return 'direct';
                } else if (fileSize < availableMemory * 0.8) {
                    return 'chunked';
                } else if (fileSizeGB < 3) {
                    return 'streaming';
                } else if (fileSizeGB < 5) {
                    return 'streaming'; // 避免Worker
                } else {
                    return 'unsupported';
                }
            }

            formatFileSize(bytes) {
                if (bytes === 0) return '0 Bytes';
                const k = 1024;
                const sizes = ['Bytes', 'KB', 'MB', 'GB'];
                const i = Math.floor(Math.log(bytes) / Math.log(k));
                return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
            }
        }

        const testOptimizer = new TestOptimizer();

        function addResult(message, type = 'info') {
            const results = document.getElementById('testResults');
            const div = document.createElement('div');
            div.className = `test-result ${type}`;
            div.innerHTML = `<strong>${new Date().toLocaleTimeString()}</strong>: ${message}`;
            results.appendChild(div);
        }

        function testStrategySelection() {
            addResult('开始测试策略选择...', 'info');
            
            try {
                const availableMemory = testOptimizer.getAvailableMemory();
                const testSizes = [
                    100 * 1024 * 1024,     // 100MB
                    500 * 1024 * 1024,     // 500MB
                    1024 * 1024 * 1024,    // 1GB
                    2 * 1024 * 1024 * 1024, // 2GB
                    4 * 1024 * 1024 * 1024, // 4GB
                    6 * 1024 * 1024 * 1024  // 6GB
                ];

                let results = `可用内存: ${testOptimizer.formatFileSize(availableMemory)}<br>`;
                
                testSizes.forEach(size => {
                    const strategy = testOptimizer.selectProcessingStrategy(size, availableMemory);
                    results += `${testOptimizer.formatFileSize(size)} → ${strategy}<br>`;
                });

                addResult(results, 'success');
                addResult('✅ 策略选择测试通过 - 没有选择Worker策略', 'success');
                
            } catch (error) {
                addResult(`❌ 策略选择测试失败: ${error.message}`, 'error');
            }
        }

        function testMemoryDetection() {
            addResult('开始测试内存检测...', 'info');
            
            try {
                if ('memory' in performance) {
                    const used = Math.round(performance.memory.usedJSHeapSize / 1024 / 1024);
                    const total = Math.round(performance.memory.totalJSHeapSize / 1024 / 1024);
                    const limit = Math.round(performance.memory.jsHeapSizeLimit / 1024 / 1024);
                    
                    addResult(`内存信息获取成功:<br>
                        已使用: ${used}MB<br>
                        总分配: ${total}MB<br>
                        限制: ${limit}MB<br>
                        使用率: ${((used / limit) * 100).toFixed(1)}%`, 'success');
                } else {
                    addResult('Performance Memory API 不可用，使用默认值', 'info');
                }
                
                addResult('✅ 内存检测测试通过', 'success');
                
            } catch (error) {
                addResult(`❌ 内存检测测试失败: ${error.message}`, 'error');
            }
        }

        function testWorkerAvoidance() {
            addResult('开始测试Worker规避...', 'info');
            
            try {
                const availableMemory = testOptimizer.getAvailableMemory();
                const largeFileSize = 4 * 1024 * 1024 * 1024; // 4GB
                
                const strategy = testOptimizer.selectProcessingStrategy(largeFileSize, availableMemory);
                
                if (strategy === 'worker') {
                    addResult('❌ 仍然选择了Worker策略，可能导致错误', 'error');
                } else {
                    addResult(`✅ 大文件(4GB)正确选择了 "${strategy}" 策略，避免了Worker`, 'success');
                }
                
                // 测试超大文件
                const hugeFIleSize = 6 * 1024 * 1024 * 1024; // 6GB
                const hugeStrategy = testOptimizer.selectProcessingStrategy(hugeFIleSize, availableMemory);
                
                addResult(`超大文件(6GB)策略: ${hugeStrategy}`, 'info');
                
            } catch (error) {
                addResult(`❌ Worker规避测试失败: ${error.message}`, 'error');
            }
        }

        // 页面加载时自动运行基础测试
        window.addEventListener('load', () => {
            addResult('🚀 修复验证测试开始', 'info');
            setTimeout(() => {
                testMemoryDetection();
                testStrategySelection();
                testWorkerAvoidance();
            }, 500);
        });
    </script>
</body>
</html>